package cn.ciis.basic.base;

import com.jfinal.validate.Validator;

/** @author 冉志林 */
public abstract class BaseValidator extends Validator {

  /** 校验电话号码正则表达式 */
  private static final String PHONE_PATTERN = "\\b(0(\\d{2,3})-\\d{6,9})\\b";
  /** 校验手机号码正则表达式 */
  private static final String MOBILE_PATTERN = "\\b(1[3,5,7,8,9]\\d{9})\\b";
  /** 校验电话手机号码正则表达式 */
  private static final String PHONE_MOBILE_PATTERN =
      "\\b((1[3,5,7,8,9]\\d{9})|(0(\\d{2,3})-\\d{6,9}))\\b";
  /** 校验MAC地址正则表达式 */
  private static final String MAC_PATTERN = "^([0-9a-fA-F]{2})(([/\\s:-][0-9a-fA-F]{2}){5})$";
  /** 校验IP地址正则表达式 */
  private static final String IP_PATTERN =
      "^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$";

  /** 校验全数字 */
  private static final String IS_NAN = "^[0-9]*$";

  /** 是否为汉字 */
  private static final String IS_CHINESE_CHARACTER = "^[\\u4e00-\\u9fa5]{0,}$";

  /** 强密码(必须包含大小写字母和数字的组合，不能使用特殊字符，长度在6-18之间) */
  private static final String IS_STRONG_PASSWORD = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{6,18}$";

  /**
   * 验证是否为强密码(必须包含大小写字母和数字的组合，不能使用特殊字符，长度在6-18之间)
   *
   * @param field
   * @param "msg"
   * @param msgString
   */
  protected void validateStrongPassword(String field,String... msgString) {
	  String msg="";
		for (int i = 0; i < msgString.length; i++) {
			msg += msgString[i];
		}
    validateRegex(field, IS_STRONG_PASSWORD, false, "msg", msg+"非强密码(必须包含大小写字母和数字的组合，不能使用特殊字符，长度在6-18之间)");
  }
  /**
   * 验证是否为全汉字
   *
   * @param field
   * @param "msg"
   * @param msgString
   */
  protected void validateChineseCharacter(String field,String... msgString) {
	  String msg="";
		for (int i = 0; i < msgString.length; i++) {
			msg += msgString[i];
		}
    validateRegex(field, IS_CHINESE_CHARACTER, false, "msg", msg+"请确定是否全为汉字");
  }

  /**
   * 验证全数字
   *
   * @param field
   * @param "msg"
   * @param msgString
   */
  protected void validateNaN(String field,String... msgString) {
	  String msg="";
		for (int i = 0; i < msgString.length; i++) {
			msg += msgString[i];
		}
    validateRegex(field, IS_NAN, false, "msg", msg+"当前含有非数字的字符");
  }
  /**
   * 校验电话号码
   *
   * @param field
   * @param "msg"
   * @param msgString
   */
  protected void validatePhonePattern(String field,String... msgString) {
	  String msg="";
		for (int i = 0; i < msgString.length; i++) {
			msg += msgString[i];
		}
    validateRegex(field, PHONE_PATTERN, false, "msg", msg+"电话号码不正确，请输入正确的电话号码");
  }

  /**
   * 验证手机号码
   *
   * @param field
   * @param "msg"
   * @param msgString
   */
  protected void validateMobilePattern(String field,String... msgString) {
	  String msg="";
		for (int i = 0; i < msgString.length; i++) {
			msg += msgString[i];
		}
    validateRegex(field, MOBILE_PATTERN, false, "msg", msg+"手机号码不正确，请输入正确的手机号码");
  }

  /**
   * 验证电话、手机号码
   *
   * @param field
   * @param "msg"
   * @param msgString
   */
  protected void validatePhoneMobilePattern(String field,String... msgString) {
	  String msg="";
		for (int i = 0; i < msgString.length; i++) {
			msg += msgString[i];
		}
    validateRegex(field, PHONE_MOBILE_PATTERN, false, "msg", msg+"电话或手机号码不正确，请输入正确的电话或手机号码");
  }

  /**
   * 验证mac
   *
   * @param field
   * @param "msg"
   * @param msgString
   */
  protected void validateMac(String field,String... msgString) {
	  String msg="";
		for (int i = 0; i < msgString.length; i++) {
			msg += msgString[i];
		}
    validateRegex(field, MAC_PATTERN, false, "msg", msg+"MAC地址不正确");
  }

  /**
   * 验证ip
   *
   * @param field
   * @param "msg"
   * @param msgString
   */
  protected void validateIp(String field,String... msgString) {
	  String msg="";
		for (int i = 0; i < msgString.length; i++) {
			msg += msgString[i];
		}
    validateRegex(field, IP_PATTERN, false, "msg", msg+"IP地址不正确,请输入正确的IP地址");
  }
  
  /**
   * 非空验证
   * @Title: validateRequired   
   * @Description: 非空验证
   * @param: @param field
   * @param: @param msgString      
   * @return: void      
   * @throws
   */
  protected void validateRequired(String field,String...msgString) {
	  String msg="";
		for (int i = 0; i < msgString.length; i++) {
			msg += msgString[i];
		}
		validateRequiredString(field, "msg", msg+"不能为空");
  }
}
